package com.feng.test.svg;

import java.awt.*;
import java.awt.geom.Point2D;
import org.apache.batik.ext.awt.LinearGradientPaint;
import org.apache.batik.ext.awt.MultipleGradientPaint;
import org.apache.batik.svggen.DefaultExtensionHandler;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGPaintDescriptor;
import org.apache.batik.svggen.SVGSyntax;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @author HuangSuip
 */
public class MyExtensionHandler extends DefaultExtensionHandler {

    public static final MultipleGradientPaint.CycleMethodEnum NO_CYCLE = new MultipleGradientPaint.CycleMethodEnum();
    public static final MultipleGradientPaint.CycleMethodEnum REFLECT_ = new MultipleGradientPaint.CycleMethodEnum();
    public static final MultipleGradientPaint.CycleMethodEnum REPEAT_ = new MultipleGradientPaint.CycleMethodEnum();
    public static final MultipleGradientPaint.ColorSpaceEnum SRGB = new MultipleGradientPaint.ColorSpaceEnum();
    public static final MultipleGradientPaint.ColorSpaceEnum LINEAR_RGB = new MultipleGradientPaint.ColorSpaceEnum();

    public SVGPaintDescriptor handlePaint(Paint paint, SVGGeneratorContext generatorCtx) {
        if (paint instanceof LinearGradientPaint) {
            LinearGradientPaint gradient = (LinearGradientPaint) paint;

            // Create a new SVG 'linearGradient' element to represent the
            // LinearGradientPaint being used.
            String id = generatorCtx.getIDGenerator().generateID("gradient");
            Document doc = generatorCtx.getDOMFactory();
            Element grad = doc.createElementNS
                    (
                            SVGSyntax.SVG_NAMESPACE_URI,
                            SVGSyntax.SVG_LINEAR_GRADIENT_TAG);

            // Set the relevant attributes on the 'linearGradient' element.
            grad.setAttributeNS(null, SVGSyntax.SVG_ID_ATTRIBUTE, id);
            grad.setAttributeNS(null, SVGSyntax.SVG_GRADIENT_UNITS_ATTRIBUTE,
                                SVGSyntax.SVG_USER_SPACE_ON_USE_VALUE);
            Point2D pt = gradient.getStartPoint();
            grad.setAttributeNS(null, "x1", String.valueOf(pt.getX()));
            grad.setAttributeNS(null, "y1", String.valueOf(pt.getY()));
            pt = gradient.getEndPoint();
            grad.setAttributeNS(null, "x2", String.valueOf(pt.getX()));
            grad.setAttributeNS(null, "y2", String.valueOf(pt.getY()));

/*            switch (gradient.getCycleMethod()) {
                case REFLECT_:
                    grad.setAttributeNS
                            (null, SVGSyntax.SVG_SPREAD_METHOD_ATTRIBUTE,
                             SVGSyntax.SVG_REFLECT_VALUE);
                    break;
                case REPEAT_:
                    grad.setAttributeNS
                            (null, SVGSyntax.SVG_SPREAD_METHOD_ATTRIBUTE,
                             SVGSyntax.SVG_REPEAT_VALUE);
                    break;
                // 'pad' is the default...
            }*/

            // Here we should write the transform of the gradient
            // in the transform attribute...

            // Here we should write the stops of the gradients as
            // children elements...

            return new SVGPaintDescriptor
                    ("url(#" + "" + ")", SVGSyntax.SVG_OPAQUE_VALUE, grad);
        } else {
            // Let the default mechanism do its job.
            return null;
        }
    }
}